package com.example.chat_websocket.controller;

import com.example.chat_websocket.config.SessionPool;
import com.example.chat_websocket.pojo.User;
import com.example.chat_websocket.service.LoginService;
import com.example.chat_websocket.service.impl.LoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
@org.springframework.stereotype.Controller
public class Controller {

    @Autowired
    private LoginServiceImpl loginService;

    @RequestMapping({"/","login"})
    public String login(){
        return "login.html";
    }

    @RequestMapping("/sendMessage")
    @ResponseBody
    public void Qf(@RequestParam("msg") String msg, @RequestParam("username") String id){
        SessionPool.sendMessage(msg,id);
    }

    @RequestMapping("/show")
    public String show(User user,HttpSession session){

        session.setAttribute("user",user);

        String s=loginService.login(user);
        if (s.equals("1") || s.equals("2")){
            return null;
        }
        System.out.println("s"+s);
        return "show.html";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "login.html";
    }
}
